<template>
  <div class="login-page">
    <van-nav-bar
      title="登录"
      left-arrow
      class="top"
      @click-left="$router.go(-1)"
    />
    <van-nav-bar class="title" title="欢迎注册" />

    <van-form @submit="onSubmit">
      <van-field
        v-model="mobile"
        name="username"
        label="用户名"
        placeholder="用户名"
        left-icon="manager"
        :rules="[
          { required: true, message: '请填写用户名' },
          { pattern: tel, message: '手机号格式不正确' }
        ]"
      />
      <van-field
        v-model="repassword"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        left-icon="lock"
        :rules="[
          { required: true, message: '请填写密码' },
          {
            validator: pwd,
            message: '密码由大于等于6位的字母数字组成'
          }
        ]"
      />
      <van-field
        v-model="password"
        left-icon="lock"
        type="password"
        name="password"
        label="确认密码"
        placeholder="确认密码"
        :rules="[
          { required: true, message: '请确认密码' },
          {
            validator: repwd,
            message: '密码由大于等于6位的字母数字组成'
          },
          {
            validator: repwd,
            message: '两次密码不一致'
          }
        ]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit" class="dlbtn"
          >注册</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { registerAPI } from '@/api/user'
export default {
  name: 'register-page',
  data () {
    return {
      mobile: '',
      password: '',
      repassword: '',
      tel: /^1\d{10}$/
    }
  },
  methods: {
    async onSubmit (values) {
      const data = await registerAPI({
        mobile: this.mobile,
        password: this.password
      })
      console.log(data)
      // this.$toast.success('注册成功')
      this.$router.push('/login')
    },
    pwd (val) {
      return /^[a-zA-Z]\w{5,}$/.test(val)
    },
    repwd (val) {
      return val === this.password
    }
  }
}
</script>

<style lang="less" scoped>
.link {
  color: #1989fa;
  font-size: 12px;
  padding-right: 20px;
  float: right;
}

.van-nav-bar {
  background-color: #1989fa;
  // 导航文字
  /deep/ .van-nav-bar__title {
    color: white;
    font-weight: 500;
    font-size: 0.42667rem;
  }
  //修改箭头颜色
  /deep/ .van-icon-arrow-left {
    color: var(--nav-bar-icon-color, #fff) !important;
  }
}

.title {
  background-color: #fff;
  /deep/ .van-nav-bar__title {
    font-size: 0.8rem;
    color: #1989fa;
    font-weight: 400;
    padding: 20px;
  }
}
</style>
